package com.yiqiu.system.controller;

import com.yiqiu.common.core.domain.entity.SysUserRole;
import com.yiqiu.common.core.domain.vo.AjaxResult;
import com.yiqiu.system.service.SysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户角色关联测试接口
 * @author weiyi
 */
@RestController
@RequestMapping("/user-role-test/")
public class Test2Controller {

    @Autowired
    private SysUserRoleService userRoleService;
    
    /**
     * 查询用户的角色关联列表
     */
    @GetMapping("/user/{userId}")
    public AjaxResult getUserRoles(@PathVariable("userId") Long userId) {
        List<SysUserRole> list = userRoleService.selectUserRoleByUserId(userId);
        return AjaxResult.success(list);
    }
    
    /**
     * 查询角色的用户关联列表
     */
    @GetMapping("/role/{roleId}")
    public AjaxResult getRoleUsers(@PathVariable("roleId") Long roleId) {
        List<SysUserRole> list = userRoleService.selectUserRoleByRoleId(roleId);
        return AjaxResult.success(list);
    }
    
    /**
     * 新增用户角色关联
     */
    @PostMapping
    public AjaxResult add(@RequestBody SysUserRole userRole) {
        int rows = userRoleService.insertUserRole(userRole);
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }
    
    /**
     * 批量新增用户角色关联
     */
    @PostMapping("/batch")
    public AjaxResult batchAdd(@RequestBody List<SysUserRole> userRoleList) {
        int rows = userRoleService.batchUserRole(userRoleList);
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }
    
    /**
     * 删除用户角色关联
     */
    @DeleteMapping
    public AjaxResult remove(@RequestBody SysUserRole userRole) {
        int rows = userRoleService.deleteUserRoleInfo(userRole);
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }
    
    /**
     * 通过用户ID删除用户角色关联
     */
    @DeleteMapping("/user/{userId}")
    public AjaxResult removeByUserId(@PathVariable("userId") Long userId) {
        int rows = userRoleService.deleteUserRoleByUserId(userId);
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }
    
    /**
     * 批量删除用户角色关联
     */
    @DeleteMapping("/users/{userIds}")
    public AjaxResult batchRemove(@PathVariable("userIds") Long[] userIds) {
        int rows = userRoleService.deleteUserRole(userIds);
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }
    
    /**
     * 批量取消授权用户角色
     */
    @DeleteMapping("/role/{roleId}/users/{userIds}")
    public AjaxResult cancelAuth(@PathVariable("roleId") Long roleId, @PathVariable("userIds") Long[] userIds) {
        int rows = userRoleService.deleteUserRoleInfos(roleId, userIds);
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }
    
    /**
     * 统计角色使用数量
     */
    @GetMapping("/count/role/{roleId}")
    public AjaxResult countUserByRole(@PathVariable("roleId") Long roleId) {
        int count = userRoleService.countUserRoleByRoleId(roleId);
        return AjaxResult.success(count);
    }
}
